var menuIndex=0;
var oriDataLength=0;
colorAdminApp.directive('repeatFinish',function(){
    return {
        link: function(scope,element,attr){
        	menuIndex=menuIndex+1;
            if(menuIndex==oriDataLength){
               	setTimeout("App.init()",2000);//ng-repeat 完成之后要初始化一下菜单
            	menuIndex=0;
            }
        }
    }
})
colorAdminApp.controller('mainController', function($cookieStore,$scope,$rootScope,$http,$state,$stateParams) {
	
          /**
          * 实现页面路由
          */
          $scope.includePage=function(stateCode,name){
              if(stateCode==undefined||stateCode=="null"||stateCode==""){
                  return ;
              }
              $scope.goState("main."+stateCode,name);
          };

          $scope.goState=function(stateCode,name){
            if(stateCode=="main.FUNCTION_MANAGE"){
                $state.go(stateCode,{type:"function","name":name});
                return;
            }else if(stateCode=="main.MENU_MANAGE"){
                $state.go(stateCode,{type:"menu","name":name});
                return;
            }
            $state.go(stateCode,{"name":name});
          };

          $scope.logout=function(){
            let entity={};
            $http.post('api/auth/oauth/consumer/token/revoke',{params:entity}).success(function(data, status, headers, config) {
                $cookieStore.put("access_token","");
                $state.go("login");
            }).error(function(data, status, headers, config) {

            });
         }

	
		$scope.initTree=function(){
			var entity={};
			$http.post('api/core/access/resources/getUserMenuZtreeJson',{params:entity}).success(function(data, status, headers, config) {
				oriDataLength=data.data.length; 
				$scope.items=$scope.convertData(data.data,0);
			}).error(function(data, status, headers, config) {
		    	alert(data);
	        });  
		};
		
	
		/**
		 * 转换菜单数据格式以便实现递归
		 */
		$scope.convertData=function(list,parentId){
			var retList=[];
			for(var i=0;i<list.length;i++){
				var t=list[i];
				if(t.pId==parentId){
					var children=$scope.convertData(list,t.id);
					if(children.length>0){
						t["hasSub"]=true;
					}else{
						t["hasSub"]=false;
					}
					t["children"]=children;
					retList.push(t);
				}
			}
			return retList;
		}
		
	   $scope.initTree();


	   $scope.getLoginUser=function(){
       			var entity={};
       			$http.post('api/core/access/user/getLoginUser',{params:entity}).success(function(data, status, headers, config) {
       				$scope.user=data.data;
       			}).error(function(data, status, headers, config) {

       	        });
       		};

       $scope.getLoginUser();


		
});


